package com.wuhan.template.module.find.activity;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.gson.Gson;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.constant.Urls;
import com.wuhan.template.module.find.adapter.WenzhengPagerAdapter;
import com.wuhan.template.module.find.bean.WenzhengBean;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class WenzhengActivity extends BaseActivity {

    @BindView(R.id.vp_wenzheng)
    ViewPager viewPager;
    private int i = 0;
    private Handler handler = new Handler() {};

    @Override
    protected void loadData() {
        OkHttpUtils.get(Urls.WZ, null);
    }

    @Override
    protected void initViews() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(i++);
                handler.postDelayed(this, 2500);
            }
        }, 2500);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                i = position;
                viewPager.setCurrentItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.wz_back)
    public void back(View view) {
        finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wenzheng_layout;
    }

    @Override
    public void onGetResult(EventMessage message) {
        super.onGetResult(message);
        if (message.type.equals(Urls.WZ)) {
            Gson gson = new Gson();
            WenzhengBean[] wenzhengBeans = gson.fromJson(message.result, WenzhengBean[].class);
            List<WenzhengBean.ContentBean.ChildsDataBean> c = wenzhengBeans[0].getContent().get(0).getChilds_data();
            WenzhengPagerAdapter adapter = new WenzhengPagerAdapter(c, this);
            viewPager.setAdapter(adapter);
        }
    }
}
